<template>
  <div
    ref="custom-table"
    class="custom-table-container"
    :class="{ 'vab-fullscreen': isFullscreen }"
  >
    <vab-query-form>
      <vab-query-form-left-panel>
        <el-form
          ref="form"
          :inline="true"
          label-width="60px"
          :model="queryForm"
          @submit.native.prevent
        >
          <el-form-item label="账号">
            <el-input v-model="queryForm.accountNumber" placeholder="账号" />
          </el-form-item>
          <el-form-item>
            <el-button
              icon="el-icon-search"
              native-type="submit"
              type="primary"
              @click="handleQuery"
            >
              查询
            </el-button>
            <el-button icon="el-icon-plus" type="primary" @click="handleAdd">
              添加
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table
      ref="tableSort"
      v-loading="listLoading"
      :border="border"
      :data="list"
      :height="height"
      :size="lineHeight"
      :stripe="stripe"
      @selection-change="setSelectRows"
    >
      <el-table-column
        align="center"
        label="序号"
        show-overflow-tooltip
        width="95"
      >
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        v-for="(item, index) in finallyColumns"
        :key="index"
        align="center"
        :label="item.label"
        :prop="item.prop"
        :sortable="item.sortable"
        :width="item.width"
      >
        <template #default="{ row }">
          <span v-if="item.label === '账号状态'">
            <el-tooltip
              :content="row[item.prop] === true ? '启用' : '禁用'"
              placement="top"
            >
              <el-switch
                v-model="row[item.prop]"
                active-color="#13ce66"
                :active-value="true"
                inactive-color="#ff4949"
                :inactive-value="false"
                @change="
                  ModifyEnable({ id: row['id'], enable: row[item.prop] })
                "
              />
            </el-tooltip>
          </span>
          <span v-else-if="item.label === '用户角色'">
            <span v-if="row[item.prop] === 1">系统管理员</span>
            <span v-else-if="row[item.prop] === 2">超级管理员</span>
            <span v-else>普通管理员</span>
          </span>
          <span v-else>{{ row[item.prop] }}</span>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        fixed
        label="操作"
        show-overflow-tooltip
        width="100"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDeleteUser(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageIndex"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <table-edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import {
    UsersQueryPage,
    ModifyUserEnable,
    DeleteUser,
  } from '@/api/sysSetting/users'
  import TableEdit from './components/userEditForm.vue'

  export default {
    name: 'CustomTable',
    components: {
      TableEdit,
    },
    data() {
      return {
        isFullscreen: false,
        border: true,
        height: this.$baseTableHeight(1),
        stripe: true,
        lineHeight: 'mini',
        checkList: [
          '用户角色',
          '账号',
          '用户名称',
          '邮箱',
          '账号状态',
          '创建时间',
        ],
        columns: [
          {
            label: 'ID',
            width: 'auto',
            prop: 'id',
          },
          {
            label: '用户角色',
            width: '200',
            prop: 'roleType',
          },
          {
            label: '账号',
            width: 'auto',
            prop: 'accountNumber',
          },
          {
            label: '用户名称',
            width: '200',
            prop: 'userName',
          },
          {
            label: '邮箱',
            width: '200',
            prop: 'email',
          },
          {
            label: '账号状态',
            width: 'auto',
            prop: 'enable',
          },
          {
            label: '创建时间',
            width: 'auto',
            prop: 'createtime',
          },
        ],
        list: [],
        imageList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageIndex: 1,
          pageSize: 20,
          accountNumber: '',
          userName: '',
        },
      }
    },
    computed: {
      finallyColumns() {
        return this.columns.filter((item) =>
          this.checkList.includes(item.label)
        )
      },
    },
    created() {
      this.fetchData()
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      handleAdd() {
        this.$refs['edit'].showEdit()
      },
      handleEdit(row) {
        this.$refs['edit'].showEdit(row)
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageIndex = val
        this.fetchData()
      },
      handleQuery() {
        this.queryForm.pageIndex = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const {
          data: { list, total },
        } = await UsersQueryPage(this.queryForm)
        this.list = list
        this.total = total
        this.listLoading = false
      },
      // 修改管理员状态
      ModifyEnable(obj) {
        ModifyUserEnable(obj).then((res) => {
          if (res.data) {
            this.$baseMessage(res.msg, 'success')
          } else {
            this.$baseMessage(res.msg, 'error')
          }
          this.fetchData()
        })
      },
      // 删除管理员信息
      handleDeleteUser(obj) {
        this.$baseConfirm('确定删除', null, async () => {
          DeleteUser({ ID: obj.id }).then((res) => {
            if (res.code === 200) {
              this.$baseMessage(res.msg, 'success')
              this.fetchData()
            } else {
              this.$baseMessage(res.msg, 'error')
            }
          })
        })
      },
    },
  }
</script>

<style lang="scss" scoped>
  .custom-table-container {
    ::v-deep {
      i {
        cursor: pointer;
      }
    }
  }
</style>
